package lxs.swift.tool.http;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


public class XFormPoster extends KVSender {

    public XFormPoster(Config config) {
        super(config, "", "&", "");
        try {
            config.header("Content-Type", "application/x-www-form-urlencoded");
            super.open();
        } catch (Exception e) {
            super.close();
            throw new RuntimeException(e);
        }
    }


    @Override
    public byte[] toBytes(String key, Object value) {
        if (key == null || key.isEmpty() || value == null) return new byte[0];
        try {
            String encode = URLEncoder.encode(value.toString(), "utf8");
            String item = URLEncoder.encode(key, "utf8") + "=" + encode;
            return item.getBytes(super.config.charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
